package cn.tedu;
//封装工具类
import java.sql.*;

public class jdbcUtility {
    /**
     * 获取 数据库的连接
     * @return Connection
     * @throws Exception
     */
    public static Connection getConnection() throws Exception{
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        String url="jdbc:mysql://localhost:3306/jdbctest?characterEncoding=utf8";
        String name="root";
        String pwd="root";
        Connection conn = DriverManager.getConnection(url,name,pwd);
        return conn;
    }

    /**
     * 关闭JDBC的资源
     * @param rs 结果集
     * @param ps 传输器
     * @param conn 连接
     */
    public static void close(ResultSet rs, PreparedStatement ps,Connection conn){
        if(rs != null){//为了防止空指针异常
            try{
                rs.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally { //为了close时又异常
                rs = null;
            }
        }
        if(ps != null){//为了防止空指针异常
            try{
                ps.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally { //为了close时又异常
                ps = null;
            }
        }
        if(conn != null){//为了防止空指针异常
            try{
                conn.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally { //为了close时又异常
                conn = null;
            }
        }
    }


}

}
